﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Caching;

namespace iMobileS.Models
{
    public interface ICache
    {
        void Add(string key, object value);
        void Add(string key, object value, TimeSpan timeout);
        void Add(string key, object value, CacheDependency dependency);
        void Set(string key, object value);
        void Set(string key, object value, TimeSpan timeout);
        bool Contains(string key);
        void Flush();
        object Get(string key);
        void Remove(string key);
    }

    public class HttpRuntimeCacheResolver : ICache
    {
        #region ICache Members

        public void Add(string key, object value, TimeSpan timeout)
        {
            HttpRuntime.Cache.Insert(key, value);
        }

        public virtual void Add(string key, object value)
        {
            HttpRuntime.Cache.Insert(key, value);
        }

        public void Add(string key, object value, CacheDependency dependency)
        {
            HttpRuntime.Cache.Insert(key, value, dependency);
        }

        public bool Contains(string key)
        {
            return HttpRuntime.Cache[key] != null;
            //return false;
        }

        public void Flush()
        {

        }

        public object Get(string key)
        {
            return HttpRuntime.Cache[key];
            //return null;
        }

        public void Remove(string key)
        {

        }

        public void Set(string key, object value, TimeSpan timeout)
        {
            HttpRuntime.Cache[key] = value;
        }

        public void Set(string key, object value)
        {
            HttpRuntime.Cache[key] = value;
        }

        #endregion
    }

    public class HttpSessionStateCacheResolver : ICache
    {
        System.Web.SessionState.HttpSessionState session;
        public HttpSessionStateCacheResolver()
        {
            //Condition.ExceptionIfNull(HttpContext.Current.Session
            session = HttpContext.Current.Session;
        }

        #region ICacher Members

        public void Add(string key, object value)
        {
            this.RegetSession();
            session.Add(key, value);
        }

        public void Add(string key, object value, CacheDependency dependency)
        {
            this.RegetSession();
            session.Add(key, value);
        }

        public void Add(string key, object value, TimeSpan timeout)
        {
            this.RegetSession();
            session.Add(key, value);
        }

        public bool Contains(string key)
        {
            this.RegetSession();
            return session[key] != null;
        }

        public void Flush()
        {
            this.RegetSession();
            session.Abandon();
        }

        public object Get(string key)
        {
            this.RegetSession();
            return session[key];
        }

        public void Remove(string key)
        {
            this.RegetSession();
            session.Remove(key);
        }

        public void Set(string key, object value)
        {
            if (this.Contains(key))
            {
                this.RegetSession();
                session[key] = value;
            }
        }

        public void Set(string key, object value, TimeSpan timeout)
        {
            this.Set(key, value);
        }

        #endregion

        /// <summary>
        /// Re-Get Session object. At the first load of Application, the 
        /// SessionState has not initialized yet. 
        /// </summary>
        private void RegetSession()
        {
            if (this.session == null)
                this.session = HttpContext.Current.Session;
            if (this.session == null)
                throw new Exception("HttpSessionState is null");
        }
    }

    public class HttpRequestCacheResolver : ICache
    {
        //System.Web.HttpRequest request;
        public HttpRequestCacheResolver()
        {
            //Condition.ExceptionIfNull(HttpContext.Current.Session
            //request = HttpContext.Current.Request;
        }

        #region ICacher Members

        public void Add(string key, object value)
        {
            HttpContext.Current.Items[key] = value;
        }

        public void Add(string key, object value, TimeSpan timeout)
        {
            this.Add(key, value);
        }

        public void Add(string key, object value, CacheDependency dependency)
        {
            this.Add(key, value);
        }

        public bool Contains(string key)
        {
            return HttpContext.Current.Items[key] != null;
        }

        public void Flush()
        {

        }

        public object Get(string key)
        {
            return HttpContext.Current.Items[key];
        }

        public void Remove(string key)
        {
            HttpContext.Current.Items.Remove(key);
        }

        public void Set(string key, object value)
        {
            HttpContext.Current.Items[key] = value;
        }

        public void Set(string key, object value, TimeSpan timeout)
        {
            this.Set(key, value);
        }
        #endregion
    }
}